module ecs{
    /**
     * 表示右手3x3浮点矩阵，它可以存储平移、缩放和旋转信息
     */
    export class Matrix2D implements IEquatable<Matrix2D>{
        public M11: number;
        public M12: number;

        public M21: number;
        public M22: number;

        public M31: number;
        public M32: number;

        private static _identity = new Matrix2D(1, 0, 0, 1, 0, 0);

        public static get Identity(){
            return this._identity;
        }

        constructor(m11: number, m12: number, m21: number, m22:number, m31: number, m32: number){
            this.M11 = m11;
            this.M12 = m12;
            this.M21 = m21;
            this.M22 = m22;
            this.M31 = m31;
            this.M32 = m32;
        }

        /**
         * 创建变换
         * @param xPosition X坐标平移
         * @param yPosition y坐标平移
         * @param result 转换作为输出参数
         */
        public static CreateTranslation(xPosition: number, yPosition: number, result: Matrix2D): Matrix2D{
            result.M11 = 1;
            result.M12 = 0;

            result.M21 = 0;
            result.M22 = 1;

            result.M31 = xPosition;
            result.M32 = yPosition;

            return result;
        }

        /**
         * 
         * @param radians 
         * @param result 
         */
        public static CreateRotation(radians: number, result: Matrix2D): Matrix2D{
            result = Matrix2D._identity;

            var val1 = Math.cos(radians);
            var val2 = Math.sin(radians);

            result.M11 = val1;
            result.M12 = val2;
            result.M21 = -val2;
            result.M22 = val1;

            return result;
        }

        public static CreateScale(xScale: number, yScale: number, result: Matrix2D): Matrix2D{
            result.M11 = xScale;
            result.M12 = 0;

            result.M21 = 0;
            result.M22 = yScale;

            result.M31 = 0;
            result.M32 = 0;

            return result;
        }

        public static Multiply(matrix1: Matrix2D, matrix2: Matrix2D, result: Matrix2D){
            var m11 = ( matrix1.M11 * matrix2.M11 ) + ( matrix1.M12 * matrix2.M21 );
			var m12 = ( matrix1.M11 * matrix2.M12 ) + ( matrix1.M12 * matrix2.M22 );

			var m21 = ( matrix1.M21 * matrix2.M11 ) + ( matrix1.M22 * matrix2.M21 );
			var m22 = ( matrix1.M21 * matrix2.M12 ) + ( matrix1.M22 * matrix2.M22 );

			var m31 = ( matrix1.M31 * matrix2.M11 ) + ( matrix1.M32 * matrix2.M21 ) + matrix2.M31;
			var m32 = ( matrix1.M31 * matrix2.M12 ) + ( matrix1.M32 * matrix2.M22 ) + matrix2.M32;

			result.M11 = m11;
			result.M12 = m12;

			result.M21 = m21;
			result.M22 = m22;

			result.M31 = m31;
            result.M32 = m32;
            
            return [matrix1, matrix2, result];
        }

        /**
         * 比较当前实例是否等于指定的值
         * @param other 
         */
        public Equals(other: Matrix2D): boolean {
            return this == other;
        }
    }
}